"""Header_Information
------------------------------------
    File_Name: demo_tortoise
    Author: RR9
    Date: 2024/10/08
    Description: 
------------------------------------
"""

from tortoise import fields, models


class User(models.Model):
    """
    用户模型
    """
    id = fields.IntField(primary_key=True)
    username = fields.CharField(max_length=20, unique=True)
    name = fields.CharField(max_length=50, null=True)
    family_name = fields.CharField(max_length=50, null=True)
    category = fields.CharField(max_length=30, default="misc")
    password_hash = fields.CharField(max_length=128, null=True)
    created_at = fields.DatetimeField(auto_now_add=True)
    modified_at = fields.DatetimeField(auto_now=True)

    def full_name(self):
        """
        返回用户名称
        """
        if self.name or self.family_name:
            return f"{self.name or ' '} ({self.family_name or ' '})".strip()
        return self.username

    class PydanticMeta:
        computed = ["full_name"]
        exclude = ["password_hash"]
